package Hxw3DEngine.core.materials.shader 
{
	/**
	 * ...
	 * @author lighter
	 */
	
	import flash.display3D.Context3D;
	import flash.utils.ByteArray;
	import Hxw3DEngine.core.Resource.geometry.ResourceGeometry;
	import Hxw3DEngine.core.materials.shader.utils.VcRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterCache;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterElement;
	import Hxw3DEngine.core.materials.shader.Shader3D;
	import Hxw3DEngine.core.Resource.texture.ResourceTexture;
	import Hxw3DEngine.core.materials.shader.filters.TextureMapFilter;
	import Hxw3DEngine.core.materials.shader.filters.SkeletonFilterBase;
	import Hxw3DEngine.core.materials.shader.filters.SkeletonFilterQuat;
	import Hxw3DEngine.core.materials.shader.filters.SkeletonFilter34;
	 
	public class SkinnedShader extends Shader3D 
	{
		private static var _instance : SkinnedShader;
		
		
		static private const _MatrixLen:uint = 12*4;
		static private const _DualQuatLen:uint = 8*4;
		static private const _TotalDualQuat:uint = 52;
		static private const _TotalMatrix:uint = 34;

		static public const _JointPoseMatrixLen:uint = 12 * 4;
		
		
		static public var _useDualQuat:Boolean = true;
		static public var _JointSkinDataLen:uint = _useDualQuat?_DualQuatLen:_MatrixLen;
		static public var _ConstLength:uint = _useDualQuat?(_DualQuatLen * _TotalDualQuat):(_MatrixLen * _TotalMatrix);	
		
		private var textureMapFilter : TextureMapFilter;
		private var skinnedFilter : SkeletonFilterBase;
		
		public function SkinnedShader() 
		{
			super([]);
			
			textureMapFilter = new TextureMapFilter(null);
			addFilter(textureMapFilter);
			
			skinnedFilter = _useDualQuat?new SkeletonFilterQuat():new SkeletonFilter34();
			addFilter(skinnedFilter);
		}
		
		public function set texture(texture:ResourceTexture) : void {
			textureMapFilter.texture = texture;
		}
		
		public function tillingOffset(repeatX:Number, repeatY:Number, offsetX:Number, offsetY:Number) : void {
			textureMapFilter.offsetX = offsetX;
			textureMapFilter.offsetY = offsetY;
			textureMapFilter.repeatX = repeatX;
			textureMapFilter.repeatY = repeatY;
		}
		public function setBoneData(bytes : ByteArray, length:int) : void {
			skinnedFilter.setBoneData(bytes,length);
		}	
		
		public static function get instance() : SkinnedShader {
			if (!_instance) {
				_instance = new SkinnedShader();
			}
			return _instance;
		}
	}

}